import time
from datetime import datetime
from pynput import keyboard
from Tools import Tools


class RecordActionUtils:
    def __init__(self):
        self.key_record = dict()

    def on_press(self, key):
        curr_time = datetime.now()
        if key not in self.key_record:
            self.key_record[key] = curr_time

    def on_release(self, key):
        curr_time = datetime.now()
        if key in self.key_record:
            value = self.key_record[key]
            hold_time = round(datetime.timestamp(curr_time) - datetime.timestamp(value), 2)
            print(f"key:{key}, with hold time:{hold_time}")
            del self.key_record[key]
